package com.lsx.logisticscore.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsx.logisticscore.entity.Employee;
import com.lsx.logisticscore.entity.Log;
import com.lsx.logisticscore.entity.Product;
import com.lsx.logisticscore.entity.Ticket;
import com.lsx.logisticscore.mapper.EmployeeMapper;
import com.lsx.logisticscore.mapper.LogMapper;
import com.lsx.logisticscore.mapper.ProductMapper;
import com.lsx.logisticscore.mapper.TicketMapper;
import com.lsx.logisticscore.request.GetTicketInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class TicketService {
    @Autowired
    private TicketMapper ticketMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    public int insertTicket(GetTicketInfoVo ticketInfo){
        //核实ticket中的product
        Product product = productMapper.selectById(ticketInfo.getProdId());
        if (product == null){
            return -1;
        }else if ((!"本地".equals(ticketInfo.getTarget())) && product.getStock() < ticketInfo.getProdSum()){
            return -2;
        }
        Ticket ticket = new Ticket();
        BeanUtils.copyProperties(ticketInfo, ticket);
        //同时增加一条log 设置为待处理
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("emp_id", ticketInfo.getManagerId());
        Employee employee = employeeMapper.selectOne(wrapper);
        if (employee == null){
            return -3;
        }
        ticketMapper.insertSelective(ticket);
        Integer ticketId = ticket.getTicketId();
        Log log = new Log(ticketId, ticketInfo.getManagerId(), employee.getName(), "待处理", "本地");
        logMapper.insertSelective(log);
        // 修改库存 进货＋ 出货-
        if ("本地".equals(ticket.getTarget())) {
            product.setStock(product.getStock() + ticketInfo.getProdSum());
        }else {
            product.setStock(product.getStock() - ticketInfo.getProdSum());
        }
        productMapper.updateById(product);
        return ticketId.intValue();
    }

    public int deleteTicket(Integer ticketId){
        //如果最新状态不是待处理就不能删除
        Log latestLog = logMapper.getLatestLog(ticketId);
        if (!"待处理".equals(latestLog.getStatus())){
            return -1;
        }
        //记得把ticket log也删掉！
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.eq("ticket_id", ticketId);
        logMapper.delete(wrapper);
        // 恢复库存
        Ticket ticket = ticketMapper.selectById(ticketId);
        Product product = productMapper.selectById(ticket.getProdId());
        if ("本地".equals(ticket.getTarget())){
            product.setStock(product.getStock() - ticket.getProdSum());
        }else {
            product.setStock(product.getStock() + ticket.getProdSum());
        }
        productMapper.updateById(product);
        return ticketMapper.deleteById(ticketId);
    }

    //状态流转
    public int updateTicket(GetTicketInfoVo ticketInfo){
        // 懒得做审核了，爱咋咋地吧,全靠自觉！
        Log log = new Log();
        BeanUtils.copyProperties(ticketInfo, log);
        log.setTime(new Date());
        int result = logMapper.insertSelective(log);
        return result;
    }

    public Page<Map<String, Object>> selectTicketByEmpId(Page<Map<String, Object>> page, String empId){
        return ticketMapper.selectTicketInfoByEmpId(page, empId);
    }
}
